package com.xiaotu.spider;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.management.JMException;

import com.xiaotu.common.model.SpiderLogModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.SpringContextHolder;
import com.xiaotu.common.util.ThreadPool;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.spider.downloader.SpiderHttpClientDownloader;
import com.xiaotu.spider.monitor.MyStatusMXBean;
import com.xiaotu.spider.monitor.SpiderMonitorThread;
import com.xiaotu.spider.pipeline.SupplementPipeline;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;
import us.codecraft.webmagic.monitor.SpiderStatusMXBean;

public class SupplementRunner
{
	
	/**
	 * 手动补爬任务执行方法
	 * @param taskInfo
	 */
	public static String supplementSpiderRun(JSONObject spiderInfo)
			throws Exception
	{
		
		if (null == spiderInfo.getString("taskName")
				|| null == spiderInfo.getString("record")
				|| null == spiderInfo.getString("comment"))
		{
			throw new Exception("传入数据包含空值");
		}
		
		JSONArray spiderArray = spiderInfo.getJSONArray("targetList");
		final SpiderLogModel spiderLog = new SpiderLogModel();
		
		APageProcessor processor = SpringContextHolder.getBean(
				spiderInfo.getString("taskName") + "PageProcessor",
				APageProcessor.class);
		SupplementPipeline pipeline = SpringContextHolder
				.getBean("SupplementPipeline", SupplementPipeline.class);
		SpiderHttpClientDownloader downloader = SpringContextHolder.getBean(
				"SpiderHttpClientDownloader", SpiderHttpClientDownloader.class);
		
		spiderLog.setSeriesNo(UUIDUtils.getStringUUID());
		spiderLog.setJobType(processor.getSpiderType());
		spiderLog.setJobNature(Constants.LogConstants.TASK_NATURE_HANDLE);
		spiderLog.setStartTime(System.currentTimeMillis());
		
		processor.setTargetList(JSONArray.toList(spiderArray, JSONObject.class),
				spiderLog);
		// setRunPara(map, processor);
		
		Map<String, Object> paraMap = new HashMap<String, Object>();
		paraMap.put("pageProcessor", spiderInfo.getString("taskName"));
		paraMap.put("pipeline", "Supplement");
		paraMap.put("sleepTime", "1");
		paraMap.put("reqHeader", "1");
		// processor.setRunParaMap(paraMap);
		Spider spider = Spider.create(processor).thread(1);
		spider.setUUID(UUIDUtils.getStringUUID());
		pipeline.setSpiderInfo(spiderInfo);
		spider.addPipeline(pipeline);
		downloader.initDownloader(processor, spiderLog, paraMap);
		spider.setDownloader(downloader);
		
		SpiderMonitor spiderMonitor = new SpiderMonitor()
		{
			@Override
			protected SpiderStatusMXBean getSpiderStatusMBean(Spider spider,
					MonitorSpiderListener monitorSpiderListener)
			{
				SpiderStatusMXBean statusMXBean = new MyStatusMXBean(spider,
						monitorSpiderListener);
				SpiderMonitorThread thread = SpringContextHolder.getBean(
						"spiderMonitorThread", SpiderMonitorThread.class);
				thread.init(statusMXBean, spiderLog);
				ThreadPool.execute(thread);
				return statusMXBean;
			}
		};
		
		try
		{
			spiderMonitor.register(spider);
		}
		catch (JMException e)
		{
			throw e;
		}
		spider.start();
		return spiderLog.getSeriesNo();
	}
	
}
